<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DECP后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" >
    <link href="/iconfont/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<form class="layui-form"  method="get" style="padding-top: 10px; " id="roleTp">


</form>



<div id="test7" class="demo-transfer"></div>

<div class="layui-btn-container">
    <button type="button" class="layui-btn" lay-demotransferactive="getData">提交</button>
</div>
<script src="/js/layui/layui.all.js"></script>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['table', 'jquery', 'form','transfer','util'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var transfer = layui.transfer;
        var util = layui.util;
        var data = top.layerFrameConfig.iframeUserModify.data;
        var values=new Array();
        $.ajax({
            url: "sysResource/queryResourceByRoleId",
            data: {roleId: data.roleId},
            datatype: "json",
            type: "GET",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].LAY_CHECKED) {
                        values.push(data[i].resId);
                    }
                }
                transfer.render({
                    elem: '#test7'
                    ,data: data
                    ,id: 'key123' //定义唯一索引
                    ,title: ['未选', '已选']
                    ,value:values
                    ,showSearch: true
                    ,parseData:function (res) {
                        return {
                            "value":res.resId,
                            "title":res.resName
                            ,"checked": res.LAY_CHECKED //是否选中
                        }
                    }
                })
                form.render();
                table.render();
            },
            error: function () {
                layer.msg("请求失败");
            }
        });



        //批量办法定事件
        util.event('lay-demoTransferActive', {
            getData: function(othis){
                var getData = transfer.getData('key123'); //获取右侧数据
                var resIds=new Array();
                for(var i=0;i<getData.length;i++){
                    resIds.push(getData[i].value);
                }
                if(resIds.sort().toString()==values.sort().toString()){
                    return layer.msg("请添加角色之后再提交",{icon:5})
                }
                layer.confirm("确定提交吗?",function (index) {
                    editUserRole(resIds);
                })
            }

        });

        function editUserRole(resIds){
            $.ajax({
                url:'sysResource/sysRoleResourceSaveyBRoleId?roleId='+data.roleId+'&id='+resIds,
                type:'get',
                dataType:'json',
                success:function (sub) {
                    console.log(sub);
                    if (sub.success== "true") {
                        parent.layer.close(top.layerFrameConfig.iframeUserModify.layer_index);
                        parent.layer.msg('分配角色成功');
                    }else {
                        parent.layer.close(top.layerFrameConfig.iframeUserModify.layer_index);
                        parent.layer.msg('分配失败');
                    }
                },error:function () {
                    parent.layer.close(top.layerFrameConfig.iframeUserModify.layer_index);
                    parent.layer.msg('分配异常');
                }
            });
        }

        form.on('submit(demo1)', function (res) {
            var ids="";
            $("input:checkbox[name='roleId']:checked").each(function() { // 遍历name=test的多选框
                ids+=$(this).val()+",";

            });
            ids = ids.substring(0, ids.lastIndexOf(","));

            $.ajax({
                url:'sysUser/sysUserRoleSaveyBUserId?userId='+data.userId+'&id='+ids,
                type:'get',
                dataType:'json',
                success:function (sub) {

                    if (sub== "success") {
                        parent.layer.close(top.layerFrameConfig.iframeUserModify.layer_index);
                        parent.layer.msg('分配角色成功');
                    }else {
                        parent.layer.close(top.layerFrameConfig.iframeUserModify.layer_index);
                        parent.layer.msg('分配失败');
                    }
                },error:function () {
                    parent.layer.close(top.layerFrameConfig.iframeUserModify.layer_index);
                    parent.layer.msg('分配异常');
                }
            });

            return false;
        });
    });
</script>
</body>
</html>
